package com.macro.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.config.XhsApiConfig;
import com.macro.mall.common.exception.XhsApiException;
import com.macro.mall.dao.*;
import com.macro.mall.dto.*;
import com.macro.mall.mapper.*;
import com.macro.mall.model.*;
import com.macro.mall.model.request.SkuBatchDeleteRequest;
import com.macro.mall.model.request.SkuBatchOfflineRequest;
import com.macro.mall.model.request.SkuBatchOnlineRequest;
import com.macro.mall.model.response.FacilityListResponse;
import com.macro.mall.model.response.PoiListData;
import com.macro.mall.model.response.XhsBaseResponse;
import com.macro.mall.service.*;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.catalina.StoreManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.fasterxml.jackson.databind.type.LogicalType.Map;

/**
 * 商品管理Service实现类
 * Created by macro on 2018/4/26.
 */
@Service
@Slf4j
public class PmsProductServiceImpl implements PmsProductService {
    private static final Logger LOGGER = LoggerFactory.getLogger(PmsProductServiceImpl.class);

    @Resource
    @Lazy
    private PmsProductService pmsProductService;


    @Resource
    private PoiFacilityService poiFacilityService;

    @Resource

    private ObjectMapper objectMapper;
    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private PmsMemberPriceDao memberPriceDao;
    @Autowired
    private PmsMemberPriceMapper memberPriceMapper;
    @Autowired
    private PmsProductLadderDao productLadderDao;
    @Autowired
    private PmsProductLadderMapper productLadderMapper;
    @Autowired
    private PmsProductFullReductionDao productFullReductionDao;
    @Autowired
    private PmsProductFullReductionMapper productFullReductionMapper;
    @Autowired
    private PmsSkuStockDao skuStockDao;
    @Autowired
    private PmsSkuStockMapper skuStockMapper;
    @Autowired
    private PmsProductAttributeValueDao productAttributeValueDao;
    @Autowired
    private PmsProductAttributeValueMapper productAttributeValueMapper;
    @Autowired
    private CmsSubjectProductRelationDao subjectProductRelationDao;
    @Autowired
    private CmsSubjectProductRelationMapper subjectProductRelationMapper;
    @Autowired
    private CmsPrefrenceAreaProductRelationDao prefrenceAreaProductRelationDao;
    @Autowired
    private CmsPrefrenceAreaProductRelationMapper prefrenceAreaProductRelationMapper;
    @Autowired
    private PmsProductDao productDao;
    @Autowired
    private PmsProductVertifyRecordDao productVertifyRecordDao;

    @Value("${xhs.app-id}")
    private String appId;
    @Resource
    private XhsLoginService xhsLoginService;


    @Resource
    private ProductSyncService productSyncService;

    @Resource
    private XhsProductService xhsProductService;

    @Resource
    private OkHttpClient httpClient;

    @Resource
    private XhsApiConfig xhsApiConfig;

    @Override
    public int create(PmsProductParam productParam) {
        int count;
        //创建商品
        PmsProduct product = productParam;
        product.setId(null);
        productMapper.insertSelective(product);
        //根据促销类型设置价格：会员价格、阶梯价格、满减价格
        Long productId = product.getId();
        //会员价格
        relateAndInsertList(memberPriceDao, productParam.getMemberPriceList(), productId);
        //阶梯价格
        relateAndInsertList(productLadderDao, productParam.getProductLadderList(), productId);
        //满减价格
        relateAndInsertList(productFullReductionDao, productParam.getProductFullReductionList(), productId);
        //处理sku的编码
        handleSkuStockCode(productParam.getSkuStockList(),productId);
        //添加sku库存信息
        relateAndInsertList(skuStockDao, productParam.getSkuStockList(), productId);

        // 默认为商品创建一个sku
//        PmsSkuStock pmsSkuStock = new PmsSkuStock();
//        skuStockMapper.insert()
        //添加商品参数,添加自定义商品规格
        relateAndInsertList(productAttributeValueDao, productParam.getProductAttributeValueList(), productId);
        //关联专题
        relateAndInsertList(subjectProductRelationDao, productParam.getSubjectProductRelationList(), productId);
        //关联优选
        relateAndInsertList(prefrenceAreaProductRelationDao, productParam.getPrefrenceAreaProductRelationList(), productId);
        count = 1;
        return count;
    }

    private void handleSkuStockCode(List<PmsSkuStock> skuStockList, Long productId) {
        if(CollectionUtils.isEmpty(skuStockList))return;
        for(int i=0;i<skuStockList.size();i++){
            PmsSkuStock skuStock = skuStockList.get(i);
            if(StrUtil.isEmpty(skuStock.getSkuCode())){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                StringBuilder sb = new StringBuilder();
                //日期
                sb.append(sdf.format(new Date()));
                //四位商品id
                sb.append(String.format("%04d", productId));
                //三位索引id
                sb.append(String.format("%03d", i+1));
                skuStock.setSkuCode(sb.toString());
            }
        }
    }

    @Override
    public PmsProductResult getUpdateInfo(Long id) {
        return productDao.getUpdateInfo(id);
    }

    @Override
    public int update(Long id, PmsProductParam productParam) {
        int count;
        //更新商品信息
        PmsProduct product = productParam;
        product.setId(id);
        productMapper.updateByPrimaryKeySelective(product);
        //会员价格
        PmsMemberPriceExample pmsMemberPriceExample = new PmsMemberPriceExample();
        pmsMemberPriceExample.createCriteria().andProductIdEqualTo(id);
        memberPriceMapper.deleteByExample(pmsMemberPriceExample);
        relateAndInsertList(memberPriceDao, productParam.getMemberPriceList(), id);
        //阶梯价格
        PmsProductLadderExample ladderExample = new PmsProductLadderExample();
        ladderExample.createCriteria().andProductIdEqualTo(id);
        productLadderMapper.deleteByExample(ladderExample);
        relateAndInsertList(productLadderDao, productParam.getProductLadderList(), id);
        //满减价格
        PmsProductFullReductionExample fullReductionExample = new PmsProductFullReductionExample();
        fullReductionExample.createCriteria().andProductIdEqualTo(id);
        productFullReductionMapper.deleteByExample(fullReductionExample);
        relateAndInsertList(productFullReductionDao, productParam.getProductFullReductionList(), id);
        //修改sku库存信息
        handleUpdateSkuStockList(id, productParam);
        //修改商品参数,添加自定义商品规格
        PmsProductAttributeValueExample productAttributeValueExample = new PmsProductAttributeValueExample();
        productAttributeValueExample.createCriteria().andProductIdEqualTo(id);
        productAttributeValueMapper.deleteByExample(productAttributeValueExample);
        relateAndInsertList(productAttributeValueDao, productParam.getProductAttributeValueList(), id);
        //关联专题
        CmsSubjectProductRelationExample subjectProductRelationExample = new CmsSubjectProductRelationExample();
        subjectProductRelationExample.createCriteria().andProductIdEqualTo(id);
        subjectProductRelationMapper.deleteByExample(subjectProductRelationExample);
        relateAndInsertList(subjectProductRelationDao, productParam.getSubjectProductRelationList(), id);
        //关联优选
        CmsPrefrenceAreaProductRelationExample prefrenceAreaExample = new CmsPrefrenceAreaProductRelationExample();
        prefrenceAreaExample.createCriteria().andProductIdEqualTo(id);
        prefrenceAreaProductRelationMapper.deleteByExample(prefrenceAreaExample);
        relateAndInsertList(prefrenceAreaProductRelationDao, productParam.getPrefrenceAreaProductRelationList(), id);
        count = 1;
        return count;
    }

    private void handleUpdateSkuStockList(Long id, PmsProductParam productParam) {
        //当前的sku信息
        List<PmsSkuStock> currSkuList = productParam.getSkuStockList();
        //当前没有sku直接删除
        if(CollUtil.isEmpty(currSkuList)){
            PmsSkuStockExample skuStockExample = new PmsSkuStockExample();
            skuStockExample.createCriteria().andProductIdEqualTo(id);
            skuStockMapper.deleteByExample(skuStockExample);
            return;
        }
        //获取初始sku信息
        PmsSkuStockExample skuStockExample = new PmsSkuStockExample();
        skuStockExample.createCriteria().andProductIdEqualTo(id);
        List<PmsSkuStock> oriStuList = skuStockMapper.selectByExample(skuStockExample);
        //获取新增sku信息
        List<PmsSkuStock> insertSkuList = currSkuList.stream().filter(item->item.getId()==null).collect(Collectors.toList());
        //获取需要更新的sku信息
        List<PmsSkuStock> updateSkuList = currSkuList.stream().filter(item->item.getId()!=null).collect(Collectors.toList());
        List<Long> updateSkuIds = updateSkuList.stream().map(PmsSkuStock::getId).collect(Collectors.toList());
        //获取需要删除的sku信息
        List<PmsSkuStock> removeSkuList = oriStuList.stream().filter(item-> !updateSkuIds.contains(item.getId())).collect(Collectors.toList());
        handleSkuStockCode(insertSkuList,id);
        handleSkuStockCode(updateSkuList,id);
        //新增sku
        if(CollUtil.isNotEmpty(insertSkuList)){
            relateAndInsertList(skuStockDao, insertSkuList, id);
        }
        //删除sku
        if(CollUtil.isNotEmpty(removeSkuList)){
            List<Long> removeSkuIds = removeSkuList.stream().map(PmsSkuStock::getId).collect(Collectors.toList());
            PmsSkuStockExample removeExample = new PmsSkuStockExample();
            removeExample.createCriteria().andIdIn(removeSkuIds);
            skuStockMapper.deleteByExample(removeExample);
        }
        //修改sku
        if(CollUtil.isNotEmpty(updateSkuList)){
            for (PmsSkuStock pmsSkuStock : updateSkuList) {
                skuStockMapper.updateByPrimaryKeySelective(pmsSkuStock);
            }
        }

    }

    @Override
    public List<PmsProduct> list(PmsProductQueryParam productQueryParam, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PmsProductExample productExample = new PmsProductExample();
        PmsProductExample.Criteria criteria = productExample.createCriteria();
        criteria.andDeleteStatusEqualTo(0);
        if (productQueryParam.getPublishStatus() != null) {
            criteria.andPublishStatusEqualTo(productQueryParam.getPublishStatus());
        }
        if (productQueryParam.getVerifyStatus() != null) {
            criteria.andVerifyStatusEqualTo(productQueryParam.getVerifyStatus());
        }
        if (!StrUtil.isEmpty(productQueryParam.getKeyword())) {
            criteria.andNameLike("%" + productQueryParam.getKeyword() + "%");
        }
        if (!StrUtil.isEmpty(productQueryParam.getProductSn())) {
            criteria.andProductSnEqualTo(productQueryParam.getProductSn());
        }
        if (productQueryParam.getBrandId() != null) {
            criteria.andBrandIdEqualTo(productQueryParam.getBrandId());
        }
        if (productQueryParam.getProductCategoryId() != null) {
            criteria.andProductCategoryIdEqualTo(productQueryParam.getProductCategoryId());
        }
        return productMapper.selectByExample(productExample);
    }

    @Override
    public int updateVerifyStatus(List<Long> ids, Integer verifyStatus, String detail) {
        PmsProduct product = new PmsProduct();
        product.setVerifyStatus(verifyStatus);
        PmsProductExample example = new PmsProductExample();
        example.createCriteria().andIdIn(ids);
        List<PmsProductVertifyRecord> list = new ArrayList<>();
        int count = productMapper.updateByExampleSelective(product, example);
        //修改完审核状态后插入审核记录
        for (Long id : ids) {
            PmsProductVertifyRecord record = new PmsProductVertifyRecord();
            record.setProductId(id);
            record.setCreateTime(new Date());
            record.setDetail(detail);
            record.setStatus(verifyStatus);
            record.setVertifyMan("test");
            list.add(record);
        }
        productVertifyRecordDao.insertList(list);
        return count;
    }

    @Override
    public int updatePublishStatus(List<Long> ids, Integer publishStatus) {
        PmsProduct record = new PmsProduct();
        record.setPublishStatus(publishStatus);
        PmsProductExample example = new PmsProductExample();
        example.createCriteria().andIdIn(ids);


        // 上下架小红书商品
        if (publishStatus.equals(1)){
            // 上架
            updateSXHGoods(ids,publishStatus);

        }else {
            //下架
            updateSXHGoods(ids,publishStatus);
        }



        return productMapper.updateByExampleSelective(record, example);
    }

    private void updateSXHGoods(List<Long> ids, Integer publishStatus) {


        List<String> skuID = new ArrayList<>();
        if (publishStatus.equals(1)){

            if (!CollectionUtils.isEmpty(ids)){
                ids.stream().forEach(id -> {
                    PmsSkuStock pmsSkuStock = skuStockMapper.selectByProductKey(id);
                    skuID.add(String.valueOf(pmsSkuStock.getId()));


                    // 必须先同步商品
                    ProductSyncRequest request = new ProductSyncRequest();
                    request.setProductId(Math.toIntExact(id));
                    pmsProductService.syncProduct(request);

                });

            }
            SkuBatchOnlineRequest skuBatchOnlineRequest =new SkuBatchOnlineRequest();
            skuBatchOnlineRequest.setOutSkuIds(skuID);
            xhsProductService.batchOnlineSkus(skuBatchOnlineRequest);
        }else {
                if (!CollectionUtils.isEmpty(ids)){
                    ids.stream().forEach(id -> {
                        PmsSkuStock pmsSkuStock = skuStockMapper.selectByProductKey(id);
                        skuID.add(String.valueOf(pmsSkuStock.getId()));
                    });

                }
            SkuBatchOfflineRequest skuBatchOfflineRequest = new SkuBatchOfflineRequest();
                skuBatchOfflineRequest.setOutSkuIds(skuID);
            xhsProductService.batchOfflineSkus(skuBatchOfflineRequest);
        }
    }

    @Override
    public int updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        PmsProduct record = new PmsProduct();
        record.setRecommandStatus(recommendStatus);
        PmsProductExample example = new PmsProductExample();
        example.createCriteria().andIdIn(ids);
        return productMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateNewStatus(List<Long> ids, Integer newStatus) {
        PmsProduct record = new PmsProduct();
        record.setNewStatus(newStatus);
        PmsProductExample example = new PmsProductExample();
        example.createCriteria().andIdIn(ids);
        return productMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateDeleteStatus(List<Long> ids, Integer deleteStatus) {
        PmsProduct record = new PmsProduct();
        record.setDeleteStatus(deleteStatus);
        PmsProductExample example = new PmsProductExample();
        example.createCriteria().andIdIn(ids);

        int i = productMapper.updateByExampleSelective(record, example);
        // 在删除小红书商品
        List<PmsSkuStock> pmsSkuStocks = skuStockMapper.selectByProductKeys(ids);
        List<String> skuIds = pmsSkuStocks.stream()
                .map(PmsSkuStock::getId)
                .map(String::valueOf)
                .collect(Collectors.toList());

        SkuBatchDeleteRequest request = new SkuBatchDeleteRequest();
        request.setOutSkuIds(skuIds);
        productSyncService.batchDeleteSkus(request);
        return i;
    }

    @Override
    public List<PmsProduct> list(String keyword) {
        PmsProductExample productExample = new PmsProductExample();
        PmsProductExample.Criteria criteria = productExample.createCriteria();
        criteria.andDeleteStatusEqualTo(0);
        if(!StrUtil.isEmpty(keyword)){
            criteria.andNameLike("%" + keyword + "%");
            productExample.or().andDeleteStatusEqualTo(0).andProductSnLike("%" + keyword + "%");
        }
        return productMapper.selectByExample(productExample);
    }

    @Override
    public Boolean syncProduct(ProductSyncRequest requests) {
        String apiUrl = xhsApiConfig.getBaseUrl()+"/api/rmp/mp/deal/poi/product/upsert";

        // 1. 获取商品数据
        PmsProduct product = productMapper.getById(requests.getProductId());
        if (product == null) {
//            log.error("商品不存在，ID: {}", productId);
            return false;
        }

        // 2. 构建请求对象（蛇形命名）
        com.macro.mall.model.request.ProductSyncRequest request = new com.macro.mall.model.request.ProductSyncRequest();
        request.setOutProductId(String.valueOf(requests.getProductId())); // 示例：prod_51
        request.setName(product.getName());
        request.setShortTitle(product.getSubTitle());
        request.setDesc(product.getDescription());
        request.setProductType(1);
        request.setSettleType(2);
        request.setPath("/pages/pro-info/pro-info?id=" + product.getId());
        request.setTopImage(product.getPic());
        request.setCategoryId("65a61ed2b3f1e8000151359a");
        request.setBizCreateTime(System.currentTimeMillis() / 1000);
        request.setBizUpdateTime(System.currentTimeMillis() / 1000);

        // 查询所有门店保存在门店下
//        poiFacilityService.getPoiList()
        List<PoiInfoDTO> allPoi = poiFacilityService.findAllPoi();
        List<String> poiList = allPoi.stream().map(PoiInfoDTO::getPoi_id).collect(Collectors.toList());
        request.setPoiIdList(poiList);

        // 3. 构建SKU（价格单位：分）
        PmsSkuStock pmsSkuStock = skuStockMapper.selectByProductKey(product.getId());
        com.macro.mall.model.request.ProductSyncRequest.SkuItem skuItem = new com.macro.mall.model.request.ProductSyncRequest.SkuItem();
        skuItem.setOutSkuId(String.valueOf(pmsSkuStock.getId()));
        skuItem.setName(product.getName());
        skuItem.setSkuImage(product.getPic());
        skuItem.setOriginPrice(product.getOriginalPrice().multiply(new BigDecimal(100)).longValue());
        skuItem.setSalePrice(product.getPrice().multiply(new BigDecimal(100)).longValue());
        skuItem.setStatus(1); // 1-上架

        request.setSkus(Arrays.asList(skuItem));

        try {
            // 4. 序列化为JSON（蛇形命名，忽略null字段）
            ObjectMapper objectMapper = new ObjectMapper()
                    .setSerializationInclusion(JsonInclude.Include.NON_NULL);
            String jsonBody = objectMapper.writeValueAsString(request);
            log.debug("请求体 JSON: {}", jsonBody);

            // 5. 获取Token
            String validToken = xhsLoginService.getValidToken();

            // 6. 发送请求
            Request httpRequest = new Request.Builder()
                    .url(apiUrl + "?app_id=" + xhsApiConfig.getAppId() + "&access_token=" + validToken)
                    .post(RequestBody.create(jsonBody, MediaType.parse("application/json")))
                    .build();

            try (Response response = httpClient.newCall(httpRequest).execute()) {
                if (!response.isSuccessful()) {
                    log.error("同步失败，状态码: {}", response.code());
                    return false;
                }
                log.info("同步成功，响应: {}", response.body().string());
                productMapper.updateStatus(product.getId());
                return true;
            }
        } catch (Exception e) {
            log.error("同步异常", e);
            return false;
        }
    }

    @Override
    public PmsProduct getById(Integer id) {
        PmsProduct product =    productMapper.getById(id);
        return product;
    }

    @Override
    public List<PmsProduct> listActiveProducts() {
        PmsProductExample productExample = new PmsProductExample();
        PmsProductExample.Criteria criteria = productExample.createCriteria();
        criteria.andDeleteStatusEqualTo(0); // 查询未删除的商品
        return productMapper.selectByExample(productExample);
    }

    /**
     * 建立和插入关系表操作
     *
     * @param dao       可以操作的dao
     * @param dataList  要插入的数据
     * @param productId 建立关系的id
     */
    private void relateAndInsertList(Object dao, List dataList, Long productId) {
        try {
            if (CollectionUtils.isEmpty(dataList)) return;
            for (Object item : dataList) {
                Method setId = item.getClass().getMethod("setId", Long.class);
                setId.invoke(item, (Long) null);
                Method setProductId = item.getClass().getMethod("setProductId", Long.class);
                setProductId.invoke(item, productId);
            }
            Method insertList = dao.getClass().getMethod("insertList", List.class);
            insertList.invoke(dao, dataList);
        } catch (Exception e) {
            LOGGER.warn("创建商品出错:{}", e.getMessage());
            throw new RuntimeException(e.getMessage());
        }
    }

}
